package net.huashitong.supervision.task.util;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.dao.SysRolePermissionDao;
import com.sirdc.modules.sys.entity.SysDept;
import com.sirdc.modules.sys.entity.SysRolePermission;
import com.sirdc.modules.sys.filter.DeptFilter;
import com.sirdc.modules.sys.service.DeptService;
import com.sirdc.modules.sys.service.SysDeptService;
import com.sirdc.modules.sys.service.SysTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/9/12
 * Time: 10:05
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Service
public class RoleMenuUtilsService extends StringPKService<SysRolePermission>{
    @Autowired
    private SysTableService sysTableService;
    @Autowired
    private SysRolePermissionDao sysRolePermissionDao;
    @Autowired
    private DeptService deptService;
    @Override
    protected BaseDao<SysRolePermission, String> getDao() {
        return sysRolePermissionDao;
    }

    @Override
    protected void beforeSave(SysRolePermission obj) {
        obj.setSysId(sysTableService.updateMaxSysId("t_sys_role_permission",null));
    }

    /**
     * 给所有单位某个角色批量添加菜单
     * @return
     */
    public  String addRoleMenu(){
        List<SysDept> depts = deptService.queryDeptList("/01/");
        ArrayList<String> menu = new ArrayList<>();
        menu.add("20191014100000000044");//回收站
       /* menu.add("20190821100000000030");//待办事项
        menu.add("20190822100000000034");//在办事项
        menu.add("20190822100000000035");//已完成
        menu.add("20190822100000000036");//全部
        menu.add("20190902100000000040");//催办记录*/
        for (SysDept dept : depts) {
            //菜单
            for (String s : menu) {
                SysRolePermission sysRolePermission = new SysRolePermission();
                sysRolePermission.setDeptId(dept.getSysId());
                sysRolePermission.setMenuId(s);
                sysRolePermission.setFunc("show");
                sysRolePermission.setRoleId("01");
           /*     sysRolePermission.setPermissionPre("/user/query");
                sysRolePermission.setPermission("/user/query:show");*/
                this.save(sysRolePermission);
            }
        }
        return "200";
    }

    public  String addDeptRoleMenu(DeptFilter filter){
        try {
            SysDept sysDept = deptService.getById(filter.getSysId());
            String deptRelation = sysDept.getDeptRelation();
            //角色
            String [] roleIds={"01","02","03","04","10","20"};
            ArrayList<String> dcMenu = new ArrayList<>();
            dcMenu.add("20190819100000000029");//;立项
            dcMenu.add("20191014100000000044");//回收站
            dcMenu.add("20190821100000000030");//待办事项
            dcMenu.add("20190822100000000034");//在办事项
            dcMenu.add("20190822100000000035");//已完成
            dcMenu.add("20190822100000000036");//全部
            dcMenu.add("20190902100000000040");//催办记录
            ArrayList<String> dwMenu = new ArrayList<>();
            dwMenu.add("20190821100000000033");
            dwMenu.add("20190825100000000037");
            dwMenu.add("20190825100000000038");
            dwMenu.add("20190825100000000039");
            dwMenu.add("20190902100000000041");
            dwMenu.add("20190929100000000043");
            if ("/01/".equals(deptRelation)){
                for (String roleId : roleIds) {
                    if ("02".equals(roleId)||"03".equals(roleId)||"04".equals(roleId)){
                        for (String s : dcMenu) {
                            SysRolePermission sysRolePermission = new SysRolePermission();
                            sysRolePermission.setDeptId(sysDept.getSysId());
                            sysRolePermission.setMenuId(s);
                            sysRolePermission.setFunc("show");
                            sysRolePermission.setRoleId(roleId);
                            this.save(sysRolePermission);
                        }
                        if ("02".equals(roleId)){
                            SysRolePermission sysRolePermission = new SysRolePermission();
                            sysRolePermission.setDeptId(sysDept.getSysId());
                            //用户管理.主要领导多一个菜单
                            sysRolePermission.setMenuId("20190911100000000042");
                            sysRolePermission.setFunc("show");
                            sysRolePermission.setRoleId(roleId);
                            this.save(sysRolePermission);
                        }
                    }
                }
            } else if ("/02/201/".equals(deptRelation)||"/02/202/".equals(deptRelation)) {
                //承办单位
                for (String roleId : roleIds) {
                    if ("02".equals(roleId)||"03".equals(roleId)||"04".equals(roleId)){
                        for (String s : dcMenu) {
                            SysRolePermission sysRolePermission = new SysRolePermission();
                            sysRolePermission.setDeptId(sysDept.getSysId());
                            sysRolePermission.setMenuId(s);
                            sysRolePermission.setFunc("show");
                            sysRolePermission.setRoleId(roleId);
                            this.save(sysRolePermission);
                        }
                        if ("02".equals(roleId)){
                            SysRolePermission sysRolePermission = new SysRolePermission();
                            sysRolePermission.setDeptId(sysDept.getSysId());
                            //用户管理.主要领导多一个菜单
                            sysRolePermission.setMenuId("20190911100000000042");
                            sysRolePermission.setFunc("show");
                            sysRolePermission.setRoleId(roleId);
                            this.save(sysRolePermission);
                        }
                    }
                }
            }
            return "200";
        } catch (Exception e) {
            e.printStackTrace();
            return "500";
        }
    }
}
